package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<RoleVo>> getByPage(@RequestBody RoleDto roleDto, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageResponse<RoleVo> pageResponse = roleService.getByPage(roleDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }

    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<Set<String>> getById(@PathVariable Long roleId) {
        return ResponseResult.success(roleService.getById(roleId));
    }

    @PostMapping("/init-roles")
    public ResponseResult<List<RoleVo>> list() {
        return ResponseResult.success(roleService.list());
    }

    @DeleteMapping("/{roleId}")
    public ResponseResult delete(@PathVariable Long roleId) {
        roleService.delete(roleId);
        return ResponseResult.success();
    }

    @PatchMapping
    public ResponseResult<Boolean> update(@RequestBody RoleDto roleDto) {
        Boolean flag = roleService.update(roleDto);
        return ResponseResult.success(flag);
    }

    @PutMapping
    public ResponseResult createRole(@RequestBody RoleDto roleDto) {
        roleService.add(roleDto);
        return ResponseResult.success();
    }
}
